﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using ThoiTrang.Data;
using System.Text;

public partial class adm_country : System.Web.UI.Page
{
    #region "Page functions"

    public string ckid = "";

    private void _BindGreeting()
    {
        a_User au=a_UserService.Geta_User(Request.Params["ckid"]);
        if (au != null)
        {
            this.lblGretting.Text = "Hi " + au.Firstname + " " + au.Lastname;
        }
    }

    private void _ReadParam()
    {
        try
        {
            if (Request.Params["catid"] != null && Request.Params["act"]==null)
            {
                a_Country acy = a_CountryService.Geta_Country(int.Parse(Request.Params["catid"]));
                if (acy != null)
                {
                    this.txtBrandEN.Text = acy.CountryNameEN;
                    this.txtBrandVN.Text = acy.CountryNameVN;
                    this.btnUpdate.Visible = true;
                    this._BindRepeater();
                }
            }
            else if (Request.Params["catid"] != null && Request.Params["act"] == "del")
            {
                //Kiem tra xem co category nao khong?
                a_CountryService.Deletea_Country(int.Parse(Request.Params["catid"]));
                CommonFunc.ShowAlertMessage(this.lblAlert,"Delete sucessfully");
                this._BindRepeater();
            }
            else if (Request.Params["fn"] != null)
            {
                this._BindRepeater();
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }

    public string GetActiveClass(string fn)
    {
        if (Request.Params["fn"] != null && Request.Params["fn"] == fn)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    public string GetActiveClassAll()
    {
        if (Request.Params["fn"] == null)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    public string _CreateLinkforAll()
    {
        if (Request.Params["ckid"] != null)
        {
            return "adm_country.aspx?ckid=" + Request.Params["ckid"].ToString();
        }
        else
        {
            return "";
        }
    }

    private void _BindRepeater()
    {
        if (Request.Params["fn"] == null | (Request.Params["fn"] == "#" | Request.Params["fn"] == ""))
        {
            this.rptCountry.DataSource = a_CountryService.Geta_CountryList(a_CountryColumns.CountryNameVN, OrderDirection.ASC);
            this.rptCountry.DataBind();
        }
        else
        {
            //Thuc hien search
            string fn = Request.Params["fn"].ToString();
            this.rptCountry.DataSource = a_CountryService.Geta_CountryList_alphabet(fn, a_CountryColumns.CountryNameVN, OrderDirection.ASC);
            this.rptCountry.DataBind();
        }
    }

    private void _BindAlphabetSearcher()
    {
        DataTable dt = General.ExecSQLStatement("SELECT DISTINCT SUBSTRING(CountryNameVN, 1, 1) AS Name FROM a_Countries ORDER BY Name");
        this.rptAlphabetSearch.DataSource = dt;
        this.rptAlphabetSearch.DataBind();
    }

    public string _CreateLinkEditAndDelete(Int32 catid)
    {
        string hLink = "";
        if (Request.Params["ckid"] != null)
        {
            hLink = "<a href='adm_country.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "&fn={0}'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a onclick=" + "\"" + "return confirm('Are you sure want to delete?')" + "\"" + "' href='adm_country.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "&act=del" + "&fn={0}'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a>";
        }
        else
        {
            hLink= "<a href='adm_country.aspx?catid=" + catid.ToString() + "&fn={0}'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a href='adm_country.aspx?catid=" + catid.ToString() + "&act=del" + "&fn={0}'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a>";
        }
        if (Request.Params["fn"] != null && (Request.Params["fn"] != "#" | Request.Params["fn"] != ""))
        {
            hLink = hLink.Replace("{0}", Request.Params["fn"].ToString());
        }
        else
        {
            hLink = hLink.Replace("{0}", "");
        }
        return hLink;
    }

    #endregion

    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["ckid"] != null)
        {
            ckid = Request.Params["ckid"];
        }
        if (!this.IsPostBack)
        {
            this._BindRepeater();
            this._ReadParam();
            this._BindGreeting();
            this._BindAlphabetSearcher();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            a_Country acy = new a_Country();
            acy.CountryNameEN = this.txtBrandEN.Text;
            acy.CountryNameVN = this.txtBrandVN.Text;
            a_CountryService.Createa_Country(acy);
            CommonFunc.ShowAlertMessage(this.lblAlert, "Add new country sucessfully");
            this._BindRepeater();
        }
        catch (Exception ex)
        {
            //throw new error
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            a_Country acy = a_CountryService.Geta_Country(int.Parse(Request.Params["catid"]));
            if (acy != null)
            {
                acy.CountryNameEN = this.txtBrandEN.Text;
                acy.CountryNameVN = this.txtBrandVN.Text;
                this.btnUpdate.Visible = false;
                a_CountryService.Updatea_Country(acy);
                CommonFunc.ShowAlertMessage(this.lblAlert, "Update country sucessfully");
                this._BindRepeater();
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }
    #endregion 
}
